<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Vue父子组件(子组件向父组件传值)</title>
    <script src="vue.js"></script>
    <style>
        .itemStyle {
            border: 1px dashed black;
            border-radius: 15px;
            padding: 10px;
            width: 200px;
            height: 280px;
            text-align: center;
            float: left;
            margin-right: 50px;
            margin-bottom: 50px;
            box-shadow: 10px 10px 5px dimgray;
            background: -webkit-gradient(linear, left top, right bottom, color-stop(0%, green), color-stop(100%, yellow));
            /* background-image: linear-gradient(to bottom right, pink, red); */
        }

        .itemFont {
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
            line-height: 32px;
        }
    </style>
</head>

<body>
    <template id="listTemplate">
        <div>
            <div class="itemFont" style="text-align: right;color: firebrick;">
                {{msg}}
            </div>
            <div>
                <lessionitem pic="images/model1.jpg" title="第四阶段：Web前端框架" desc="知识要点：Bootstrap前端框架、VUE前端框架"
                    headimg="images/tt.jpg" tname="王五" count="35" @receive="getValue"></lessionitem>

                <lessionitem pic="images/model2.png" title="第三阶段：企业级框架扩展" desc="知识要点：Shiro权限认证框架、SpringCloud微服务框架、JFinal一站式框架、JVM内存模型、垃圾回收算法、内存监控
                课程目标：利用Shiro进行权限认证，掌握SpringCloud框架，利用JFinal进行WEB应用开发，了解JVM内存模型和垃圾回收算法" headimg="images/端午节合照.jpg"
                    tname="赵六" count="45" @receive="getValue"></lessionitem>

                <lessionitem pic="images/model16.jpg" title="第一阶段：JavaSE核心编程"
                    desc="知识要点：环境搭建，基本数据类型，变量及去声明，运算符，流程控制语句，IDE绘制界面" headimg="images/头.jpg" tname="李四" count="27"
                    @receive="getValue"></lessionitem>

                <lessionitem pic="images/model15.jpg" title="第四阶段：Redis缓存" desc="知识要点：Redis缓存数据库的应用、Java基于Redis的应用开发
                课程目标：掌握NOSQL数据库Redis的安装、使用，Redis客户端的安装使用，Java访问操作Redis数据库，Redis的持久化方案、主从复制、高可用。"
                    headimg="images/wuguangdong.jpg" tname="老吴" count="16" @receive="getValue"></lessionitem>

                <lessionitem pic="images/model14.jpg" title="第四阶段：分布式与微服务项目"
                    desc="知识要点：项目文档，项目规范，需求分析，数据库设计，工程构建，需求评审，配置管理，BUG修复，项目管理等" headimg="images/hanling.jpg" tname="韩玲"
                    count="35" @receive="getValue"></lessionitem>
            </div>
        </div>

    </template>

    <template id="itemTemplate">
        <div class="itemStyle">
            <div>
                <img :src="pic" width="200px" height="120px">
            </div>

            <div>
                <div class="itemFont">
                    {{title}}
                </div>
                <div class="itemFont">
                    {{desc}}
                </div>
                <div class="itemFont" style="text-align: right;color: firebrick;">
                    <a @click=appointment>预约课程</a>
                </div>

                <div>
                    <img :src="headimg" width="22px" height="22px">
                    <span>{{tname}}</span>
                    <span>{{count}}</span>
                </div>
            </div>
        </div>
    </template>

    <div id="app1">
        <lessionlist></lessionlist>
    </div>

</body>

</html>
<script>

    var vm1 = new Vue({
        el: "#app1",
        components: {
            "lessionlist": {
                template: "#listTemplate",
                data() {
                    return {
                        msg: ""
                    }
                },
                methods: {
                    getValue(val) {
                        var content = val;
                        var array = content.split(",");
                        var tname = array[0];
                        var title = array[1];
                        var count = parseInt(array[2]);
                        var message = "你已经成功预约了" + tname + "老师的" + title + "目前该课程已成功预约了" + parseInt(count + 1) + "人";
                        this.msg = message;
                    }
                },
                components: {
                    "lessionitem": {
                        template: "#itemTemplate",
                        //数组中变量不能写大写
                        props: ["pic", "title", "desc", "headimg", "tname", "count"],
                        methods: {
                            appointment() {
                                var tname = this.tname;
                                var title = this.title;
                                var count = this.count;
                                var content = tname + "," + title + "," + count;
                                // 通过$emit方法将子组件中的内容可以传递到父组件，该方法的第一个参数是自定义事件，第2个参数则是子组件中的数据
                                this.$emit("receive", content);
                            }
                        },
                    }
                }
            }
        }
    });
</script>